<?php
/*
 * 活动
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\Session;
use app\common\model\Activity as ActivityModel;

class Activity extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function lists()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){$query->where('name','LIKE', '%'.trim($param['title']).'%');};
        }else{
           $where = 1;
        }
        $list = ActivityModel::where($where)->paginate();
        return $this->fetch('index',['list'=>$list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = ActivityModel::create($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('添加了活动');
            $this->success('添加成功');
        }
        return $this->fetch('save');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = ActivityModel::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('修改了活动');
            $this->success('修改成功');
        }
        return $this->fetch('save', ['data' => Db::table('activity')->find(input('id'))]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            ActivityModel::destroy($param['id']);
            insert_admin_log('删除了活动');
            $this->success('删除成功');
        }
    }



}
